package com.openess.bigsearch.engine.DAO;

import java.io.IOException;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.util.Bytes;

import com.openess.bigsearch.engine.search.IndexSearcher.DocScore;
import com.openness.generator.IdGenerator;
import com.openness.generator.IdGeneratorConfig;
import com.openness.spider.Html;
import com.openness.spider.api.SpiderDataReader;

public class HBaseManager implements DBManager {
	/**
	 * 一次性加载的数量
	 */
	private int max = 51;
	private static Configuration conf;
	private static IdGenerator generator;
	static {
		conf = new Configuration();
		conf.set(SpiderDataReader.HBASE_MASTER, "192.168.1.222:60010");
		conf.set(SpiderDataReader.HBASE_ZOOKEEPER_QUORUM, "192.168.1.222");
		conf.set(SpiderDataReader.HBASE_ZOOKEEPER_PROPERTY_CLIENTPORT, "2181");
		conf.set(SpiderDataReader.HBASE_TABLEPOOL_SIZE, "10");
		IdGeneratorConfig config = new IdGeneratorConfig();
		config.setConnectString("192.168.1.222:2181");
		config.setSessionTimeout(3000);
		try {
			generator = new IdGenerator(config, "test");
		} catch (Exception e) {
		}
	}

	private SpiderDataReader sreader;
	private List<Html> list;
	/**
	 * 读取计数器
	 */
	private int count;

	public HBaseManager() throws IOException {
		byte[] startRow = Bytes.toBytes("1375619427969");
		byte[] stopRow = Bytes.toBytes("1375619682030");
		sreader = new SpiderDataReader(conf, "spiderdata", startRow, stopRow,
				null);
		reload();
	}

	/**
	 * 加载数据
	 * 
	 * @throws IOException
	 */
	public void reload() throws IOException {
		list = sreader.next(max);
	}

	@Override
	public void close() throws IOException {
		list = null;
		sreader.close();
		generator.close();
	}

	@Override
	public DBData readDBData() {
		try {
			if (list != null && count == list.size() - 1) {
				count = 0;
				reload();
			}
			if (list == null)
				return null;
			Html html = list.get(count++);
			return html2DB(html);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	private DBData html2DB(Html html) {
		if (html == null)
			return null;
		String id = generator.get() + "";
		String rowkey = html.getRowkey();
		String url = html.getUrl();
		String title = html.getTextTitle();
		String keywords = html.getHtmlKeywords();
		String description = html.getHtmlDescription();
		String content = html.getTextContent();
		String interest = html.getInterest() + "";
		String category = html.getCategory() + "";
		String website = html.getWebsite() + "";
		String source = html.getTextSource();
		return new DBData(id, rowkey, url, title, keywords, description,
				content, interest, category, website, source);
	}
}
